package com.cwd.arm.application.service.write;

import com.cwd.arm.application.dto.user.UserAddDto;
import com.cwd.arm.application.dto.user.UserUpdateDto;
import com.cwd.arm.application.service.write.facade.UserWriteService;
import com.cwd.arm.common.dto.session.UserDto;
import com.cwd.arm.domain.entity.UserEntity;
import com.cwd.arm.domain.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by yan on 21-11-14.
 */
@Repository
@Primary
public class UserWriteServiceImpl implements UserWriteService{

    @Autowired
    private UserRepository userRepository;

    @Transactional
    @Override
    public void save(UserAddDto addDto) {
        userRepository.save(convertToEntity(addDto));
    }

    @Transactional
    @Override
    public void update(UserUpdateDto updateDto) {
        userRepository.update(convertToEntity(updateDto));
    }

    @Transactional
    @Override
    public void del(Long id) {
        userRepository.del(id);
    }

    private UserEntity convertToEntity(UserAddDto dto){
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(dto,userEntity);
        return userEntity;
    }

    private UserEntity convertToEntity(UserUpdateDto dto){
        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(dto,userEntity);
        return userEntity;
    }

}
